package com.situation.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situation.converter.MenuConverter;
import com.situation.mapper.MenuMapper;
import com.situation.mapper.RoleMapper;
import com.situation.mapper.TbDepartmentMapper;
import com.situation.mapper.TbUserMapper;
import com.situation.pojo.DO.*;
import com.situation.pojo.VO.MenuNodeVO;
import com.situation.pojo.VO.UserRoleVO;
import com.situation.pojo.VO.UserVO;
import com.situation.service.TbUserService;
import com.situation.util.JWTUtil;
import com.situation.util.MenuTreeBuilder;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author qian
 * @date 2021/12/7 10:34
 */
@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * @param user
     * @return
     */
    @Override
    public int addTbUser(TbUser user) {
        user.setCreateTime(LocalDateTime.now());
        user.setModifiedTime(LocalDateTime.now());
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
        user.setType(1);
        user.setStatus(1);
        user.setAvatar("http://thirdqq.qlogo.cn/g?b=oidb&k=icTYjyV5afABvE1v4ge9SLg&s=100&t=1584195695");
        SimpleHash simpleHash = new SimpleHash("md5", user.getPassword(), salt, 2);
        user.setPassword(simpleHash.toString());
        return tbUserMapper.addTbUser(user);
    }

    /***
     * @Description: 删除用户
     * @Param: [id]
     * @return: int
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public int deleteTbUser(Long id) {
        return tbUserMapper.deleteTbUser(id);
    }

    @Override
    public int updateTbUser(TbUser tbUser) {
        tbUser.setModifiedTime(LocalDateTime.now());
        return tbUserMapper.updateTbUserById(tbUser);
    }

    /**
     * 查询单个用户
     *
     * @param id
     * @return
     */
    @Override
    public UserVO queryUserVoById(Long id) {
        return tbUserMapper.queryUserVoById(id);
    }

    /***
     * @Description: 查询所有vo用户
     * @Param: []
     * @return: java.util.List<com.situation.pojo.VO.UserVo>
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public List<UserVO> queryAllUserVo() {
        return tbUserMapper.queryAllUserVo();

    }

    /***
     * @Description: 查询所有数据库用户
     * @Param: []
     * @return: java.util.List<com.situation.pojo.DO.TbUser>
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public List<TbUser> findAllTbUser() {
        return tbUserMapper.findAllTbUser();
    }

    /***
     * @Description: 所有vo用户分页
     * @Param: [pageNum, pageSize]
     * @return: com.github.pagehelper.PageInfo<com.situation.pojo.VO.UserVo>
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public PageInfo<UserVO> page(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVO> userVoList = tbUserMapper.queryAllUserVo();
        PageInfo<UserVO> pageInfo = new PageInfo<>(userVoList);
        return pageInfo;
    }

    /***
     * @Description: 查询菜单
     * @Param: [tbUser, pageNum, pageSize]
     * @return: com.github.pagehelper.PageInfo<com.situation.pojo.DO.TbMenu>
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public List<MenuNodeVO> findMenu() {

        List<TbMenu> menus = null;
        List<MenuNodeVO> menuNodeVOS = new ArrayList<>();
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        TbUser user = tbUserMapper.findUserByUsername(username);

        if (user.getType() == 0) {
            //超级管理员
            menus = menuMapper.findAllMenu();
        } else if (user.getType() == 1) {
            //普通系统用户
            menus = tbUserMapper.findMenu(username);
        }

        if (!CollectionUtils.isEmpty(menus)) {
            menuNodeVOS = MenuConverter.converterToMenuNodeVO(menus);
        }

        //构建树形菜单
        return MenuTreeBuilder.build(menuNodeVOS);
    }

    /***
     * @Description: 多条件模糊查询
     * @Param: [userVo, pageNum, pageSize]
     * @return: com.github.pagehelper.PageInfo<com.situation.pojo.VO.UserVo>
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public PageInfo<UserVO> findUserList(TbUser tbUser, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVO> userVoList = tbUserMapper.findUserVOByTbUser(tbUser);
        PageInfo<UserVO> pageInfo = new PageInfo<>(userVoList);
        return pageInfo;
    }

    /***
     * @Description: 查询用户登入信息
     * @Param: [tbUser]
     * @return: com.situation.pojo.VO.UserRoleVo
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public UserRoleVO userInfo() {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        return tbUserMapper.userInfo(username);
    }


    /***
     * @Description: 给指定id用户添加权限
     * @Param: [tbUserRole]
     * @return: int
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    @Transactional
    public int addTbUserRole(Long id, List<Long> ids) {

        // 查询 旧角色
        List<TbRole> roleById = findRoleById(id);
        // 删除 旧角色
        int i1 = tbUserMapper.deleteRolesById(id);

        // 添加 新角色
        int s = 0;
        for (Long aLong : ids) {
            int i = tbUserMapper.addTbUserRole(id, aLong);
            if (i > 0)
                s++;
        }
        return s;
    }


    /***
     * @Description: 通过用户id查询现有角色
     * @Param: [id]
     * @return: com.situation.pojo.DO.TbRole
     * @Author: 赵启岸
     * @Date: 2021/12/8
     */
    @Override
    public List<TbRole> findRoleById(Long id) {
        return tbUserMapper.findRoleById(id);
    }


    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public TbUser login(String username, String password) {
        TbUser login = tbUserMapper.login(username, password);
        return login;
    }

    @Override
    public int updateStatus(Long id, int status) {
        return tbUserMapper.updateStatus(id, status);
    }


}
